var mainProtectionTable;
var dwProtectionTable;
var uncheckedTab;
function initTab() {
	setTimePicker();
	query();
	$('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
		var title = $(e.target).html();
		if (title == localeTitle.ZhuBaoHuTuiChuQiangKuang) {
			var t1 = window.setInterval(function() {
				if ($("#actRisk").width()) {
					mainProtectionTable.getTable().draw();
					window.clearInterval(t1);
				}
			}, 50);
		} else if (title == localeTitle.DianwangBaoHuFengXianYunXing) {
			var t2 = window.setInterval(function() {
				if ($("#mainProtectQuit").width()) {
					dwProtectionTable.getTable().draw();
					window.clearInterval(t2);
				}
			}, 50);
		} else if (title == localeTitle.ChaoQiWeiJian) {
			var t3 = window.setInterval(function() {
				if ($("#uncheckedTab").width()) {
					uncheckedTab.getTable().draw();
					window.clearInterval(t3);
				}
			});
		}
	})
}
function resizeHtml() {
	mainProtectionTable.getTable().draw();
	dwProtectionTable.getTable().draw();
	uncheckedTab.getTable().draw();
}
/**
 * 查询按钮点击时间
 */
function query() {
	initdwProtection();
	initmainProtection();
	initUnCheckedRisk();
};
/**
 * 加载电网保护风险
 */
function initdwProtection() {
	var start = $('#startTime').val();
	var end = $('#endTime').val();
	$.getJSON("/rest/protectRisk/getOperationalRisks/" + start + "/" + end, function(result) {
		initDwProtectionTable(result);
	});
}
/**
 * 加载主保护退出运行情况
 */
function initmainProtection() {
	var start = $('#startTime').val();
	var end = $('#endTime').val();
	$.getJSON("/rest/protectRisk/getMainProtectionQuit/" + start + "/" + end, function(result) {
		initmainProtectionTable(result);
	});
}
function initUnCheckedRisk() {
	var start = $('#startTime').val();
	var end = $('#endTime').val();
	$.getJSON("/rest/protectRisk/getUnCheckedRisk/" + start + "/" + end, function(result) {
		initUnCheckedRiskTable(result);
	});
}

function initUnCheckedRiskTable(result) {
	if (uncheckedTab) {
		uncheckedTab.getTable().destroy();
	}
	var columns = [ localeTitle.XuHao, localeTitle.ZhiDiaoXiTong, localeTitle.GuangDongShengWang,
			localeTitle.GuangXiShengWang, localeTitle.YunNanShengWang, localeTitle.GuiZhouShengWang,
			localeTitle.HaiNanShengWang, localeTitle.ZongJi ];
	var myotp = MyTable.getDefaultOptions();
	myotp.paging = false;
	myotp.scrollY = scrollYHeight+'px';
	myotp.columnDefs = [ {
		targets : 0,
		width : '10%'
	}, {
		targets : 1,
		width : '10%'
	}, {
		targets : 2,
		width : '10%'
	}, {
		targets : 3,
		width : '10%'
	}, {
		targets : 4,
		width : '15%'
	}, {
		targets : 5,
		width : '15%'
	}, {
		targets : 6,
		width : '15%'
	}, {
		targets : 7,
		width : '15%'
	} ];
	uncheckedTab = new MyTable('uncheckedTab', columns, myotp);
	uncheckedTab.setDataSet(getUnCheckedDataset(result));
	uncheckedTab.show();
}
function getUnCheckedDataset(data) {
	var dataset = [];
	for ( var i in data) {
		var d = data[i];
		dataset.push([ 1, d.directTransferNum, d.guangdongNum, d.guangxiNum, d.yunnanNum, d.guizhouNum, d.hainanNum,
				d.sum ]);
	}
	return dataset;
}

function initDwProtectionTable(result) {
	if (dwProtectionTable) {
		dwProtectionTable.getTable().destroy();
	}
	var columns = [ localeTitle.XuHao, localeTitle.DiaoGuanFanWei, localeTitle.GongZuoShiJian,
			localeTitle.FengXianNeiRong, localeTitle.FengXianDengJi, localeTitle.XiangGuanBaoHuCuoShi,
			localeTitle.BeiZhu ];
	var myopt = MyTable.getDefaultOptions();
	myopt.paging = false;
	myopt.scrollY = scrollYHeight+'px';
	myopt.columnDefs = [ {
		targets : 0,
		width : '3%'
	}, {
		targets : 1,
		width : '5%'
	}, {
		targets : 2,
		width : '20%'
	}, {
		targets : 3,
		width : '20%'
	}, {
		targets : 4,
		width : '20%'
	}, {
		targets : 5,
		width : '20%'
	}, {
		targets : 6,
		width : '10%'
	}];
	dwProtectionTable = new MyTable('dwProtection', columns, myopt);
	dwProtectionTable.setDataSet(getDwProtectionDataset(result));
	dwProtectionTable.show();
}
function getDwProtectionDataset(data) {
	var dataset = [];
	for ( var i in data) {
		var d = data[i];
		dataset.push([ Number(i) + 1, str(d.subControlArea), str(d.workTime), str(d.riskContect), str(d.riskRating),
				cutStr(d.procedures), str(d.remark) ]);
	}
	return dataset;
}
function cutStr(str) {
	if (!str) {
		return "";
	}
	var cut = str.length > 30 ? str.substring(0, 30) + "..." : str;
	return "<span title='" + str + "'>" + cut + "</span>";
}
function getDayStr(t) {
	if (!t) {
		return "";
	}
	t = t.time ? t.time : t;
	var d = new Date(t);
	return d.getFullYear() + "-" + d.getMonth() + "-" + d.getDay();
}
function str(d) {
	return d ? d : '';
}
function initmainProtectionTable(result) {
	if (mainProtectionTable) {
		mainProtectionTable.getTable().destroy();
	}
	var columns = [ localeTitle.XuHao, localeTitle.DiaoGuanFanWei, localeTitle.ChangZhanMingCheng,
			localeTitle.BaoHuTongDaoTuiChuQingKuang, localeTitle.ZhiXingShiJian, localeTitle.TongDaoZhongDuanShiJian,
			localeTitle.BeiZhu ];
	var myopt = MyTable.getDefaultOptions();
	myopt.paging = false;
	myopt.scrollY =scrollYHeight+'px';
	myopt.columnDefs = [ {
		targets : 0,
		width : '5%'
	}, {
		targets : 1,
		width : '5%'
	}, {
		targets : 2,
		width : '7%'
	}, {
		targets : 3,
		width : '20%'
	}, {
		targets : 4,
		width : '10%'
	}, {
		targets : 5,
		width : '10%'
	}, {
		targets : 6,
		width : '10%'
	}];
	mainProtectionTable = new MyTable('mainProtection', columns, myopt);
	mainProtectionTable.setDataSet(getMainProtectionDataset(result));
	mainProtectionTable.show();
}
function getMainProtectionDataset(data) {
	var dataset = [];
	for ( var i in data) {
		var d = data[i];
		var t1 = new Date(d.executionTime.time ? d.executionTime.time : d.executionTime).toLocaleString();
		var t2 = new Date(d.channelBreakTime.time ? d.channelBreakTime.time : d.channelBreakTime).toLocaleString();
		dataset.push([ Number(i) + 1, str(d.subCrotrolArea), str(d.subStationName), str(d.channelExit), str(t1),
				str(t2), str(d.remark) ]);
	}
	return dataset;
}
function setTimePicker() {
	$('#startTime').val($('#start').val());
	$('#endTime').val($('#end').val());
	$('#startTime').datetimepicker({
		format : 'yyyy-mm-dd',
		startView : 2,
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		todayHighlight : true
	});
	$('#endTime').datetimepicker({
		format : 'yyyy-mm-dd',
		startView : 2,
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		pickerPosition : 'bottom-left',
		todayHighlight : true
	});

}
